/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.libs.AbstractLib;

import fever.data.libs.BitmapLibEvent;
import fever.data.libs.BitmapLibLocator;
import fever.display.FeverStage;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

import flash.display.BitmapData;

/**
 * BitmapLib is used to load external image and store data into a 
 * {@code BitmapData} object.
 * 
 * <p>We can retreive ( attach ) data using directly {@link #getContent} method 
 * or using {@link fever.data.libs.BitmapLibLocator} helper.
 * 
 * @see fever.data.libs.BitmapLibLocator
 * @see fever.data.libs.BitmapLibEvent
 * 
 * @author Romain Ecarnot
 */
class fever.data.libs.BitmapLib extends AbstractLib
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _container : MovieClip; 
	private var _isLoaded : Boolean;
	
	private var _repository : String;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function BitmapLib( repositoryID : String )
	{
		super();
		
		_repository = ( !repositoryID ) ? BitmapLibLocator.DEFAULT : repositoryID;
		
		_isLoaded = false;
	}
	
	/**
	 * Inits event structure.
	 * 
	 * <p>Here a {@link fever.data.libs.BitmapLibEvent} object
	 */
	public function initEventSource() : Void
	{
		_e = new BitmapLibEvent( null, this );
	}
	
	/**
	 * Starts loading bitmap dara from passed-in url adress.
	 * 
	 * @param url Image's url
	 */
	public function load( url : String ) : Void
	{
		if ( url != undefined) setURL( url );
		if( !_sName ) setName( url );
		
		release();
		
		_container = FeverStage.getInstance().createBitmapLibTarget();
		
		_container._x = -10000;
		_container._y = -10000;
		_container._visible = false;
		_container.loadMovie( super.getURL() );
		
		super.load();
	}
	
	/**
	 * Returns loaded bytes.
	 */
	public function getBytesLoaded() : Number
	{
		return _container.getBytesLoaded();
	}
	
	/**
	 * Returns total bytes to load.
	 */
	public function getBytesTotal() : Number
	{
		return _container.getBytesTotal();
	}
	
	/**
	 * Indicates if BitmapData is available or not.
	 */
	public function isLoaded() : Boolean
	{
		return _isLoaded;	
	}
	
	/**
	 * Trigerred when data il correctly loaded.
	 */
	public function onLoadInit() : Void
	{
		var bmp : BitmapData = new BitmapData( _container._width, _container._height, true, 0x00FFFFFF );
		bmp.draw( _container );

		_container.removeMovieClip();
		
		if ( _sName ) 
		{
			if ( !( BitmapLibLocator.getLocator( _repository ).isRegistredBitmap( _sName ) ) )
			{
				BitmapLibLocator.getLocator( _repository ).pushBitmap( _sName, bmp );
			} 
			else
			{
				FeverDebug.ERROR( 
					this + " can't be registered to " + 
					_repository + " repository with '" + _sName + 
					"' name. This name already exists."
				);
			}
		}
		
		_isLoaded = true;
		super.onLoadInit();
		
		release();
	}
	
	/**
	 * Release current lib.
	 * 
	 * <p>Bitmap data is disposed
	 */
	public function release() : Void
	{
		getContent().dispose();
		super.release();
	}
	
	/**
	 * Returns current content, here a {@code BitmapData} object.
	 */
	public function getContent() : BitmapData
	{
		if( BitmapLibLocator.getLocator( _repository ).isRegistredBitmap( _sName ) )
			return BitmapLibLocator.getLocator( _repository ).getBitmap( _sName );
		return null;
	}
	
	/**
	 * Sets BitmapData content.
	 * 
	 * @param bmp new BitmapData content
	 */
	public function setContent( bmp : BitmapData ) : Void
	{
		// do nothing, content is store to BitmapLocator not in instance
	}
	
	/**
	 * String representation of instance.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}

}